﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guoc.DAL.Interface;
using Guoc.DAL.Domain;
using Guoc.DAL.CastleIOC;
namespace Guoc.Web.Controllers
{

    public class HomeController : Controller
    {
        public HomeController()
            : this(null)
        { }

        public HomeController(IEmployee iemp)
        {
            //此构造函数只为单元测试用
            //正式代码通过容器来构建雇员接口
            _iemployee = iemp ?? IocFactory<IEmployee>.GetService(iemp);
        }

        public IEmployee _iemployee
        {
            get;
            private set;
        }


        public ActionResult Index()
        {
            ViewBag.begin = HttpContext.Items["begin"];
            ViewBag.end = HttpContext.Items["end"];

            return View();
        }


        public ActionResult Login()
        {
            ViewBag.Title = "Login";
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(string userName, string password, string checkCode)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return Json(new { code = 0, message = "用户名不能为空" });
            }
            if (string.IsNullOrEmpty(password))
            {
                return Json(new { code = 0, message = "密码不能为空" });
            }
            Employee emp = _iemployee.GetEmployee(userName);
            if (string.IsNullOrEmpty(emp.ID))
            {
                return Json(new { code = 0, message = "用户名不存在" });
            }

            if (emp.LoginName == userName && emp.LoginPwd == password)
            {
                Guoc.Web.Tool.Admin.SetAdmin(emp);
                return RedirectToAction("Index");
            }
            else
            {
                return Json(new { code = 0, message = "密码不正确" });
            }

        }

        public ActionResult LogOut()
        {

            return null;
        }

    }
}
